package chapter.sixth.item35;

import java.lang.reflect.Method;

public class RunTests2 {
	
	public static void main(String[] args) throws ClassNotFoundException {
		Class<?> clazz = Class.forName(args[0]);
		int tests = 0;
		int passed = 0;
		for(Method m:clazz.getDeclaredMethods()){
			if(m.isAnnotationPresent(ExceptionTest.class)){
				tests++;
				try{
					m.invoke(null);
					System.out.printf("Test %s failed: no exception%n",m);
				}catch(Throwable wrappedExc){
					Throwable exc = wrappedExc.getCause();
					Class<? extends Exception>[] excTypes = 
							m.getAnnotation(ExceptionTest.class).value();
					int oldPassed = passed;
					for(Class<? extends Exception> excType:excTypes){
						if(excType.isInstance(exc)){
							passed++;
							break;
						}
					}
					if(passed == oldPassed){
						System.out.printf("Test %s failed: %s %n",m,exc);
					}
				}
			}
		}
	}
}
